package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookMarkService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;

/**
 * 评论接口
 * @author xusaike
 *
 */
@Component
@Path("/v{version}/mark")
public class BookMarkRestController {
	
	@Resource
	private BookMarkService bookMarkService;
	/**
	 * 加入书签
	 * @param book_id
	 * @param chap_id
	 * @param chapter_offset
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/add")
	public String addMark(
			@FormParam("book_id")int book_id,
			@FormParam("chapter_id")int chap_id,
			@FormParam("chapter_offset")int chapter_offset,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookMarkService.addBookMark(book_id, chap_id, chapter_offset, token, device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 
	 * @param mark_id
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/delete")
	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) 
	public String deleteReview(
			@FormParam("mark_id")List<String> mark_ids,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookMarkService.deleteMark(mark_ids,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("mark_ids", mark_ids);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@GET
    @Path("/list")
	public String getBook(@QueryParam("book_id")int book_id,
			@QueryParam("pages")int pages,
			@QueryParam("contents")int contents,@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookMarkService.getBookMarkList(book_id,pages,contents,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	
}
